<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<%@page import="de.pernpas.calendar.common.CalUtils"%>
<%@page import="java.util.Date" %>

<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Locale"%>
<%@page import="java.util.Calendar"%>

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="de.pernpas.calendar.service.EventService"%>
<%@page import="java.util.List"%>
<%@page import="de.pernpas.calendar.entity.Event"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"">
<link type="text/css" rel="stylesheet" href="/css/stylesheet.css" />
<title>Calendar</title>
</head>
<body>
	<%
	Date now = new Date();
	GregorianCalendar cal = new GregorianCalendar();
	CalUtils cu = new CalUtils();
	int actMonth;
	int actYear;
	int prevMonth;
	int nextMonth;
	int prevYear;
	int nextYear;
	
	SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
	
	if(request.getParameter("month") != null &&
			request.getParameter("year") != null){
		actMonth = Integer.parseInt(request.getParameter("month"));
		actYear = Integer.parseInt(request.getParameter("year"));
		cal.set(Calendar.MONTH, actMonth);
		cal.set(Calendar.YEAR, actYear);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		now = cal.getTime();
	}else{
		actMonth = cal.get(Calendar.MONTH);
		actYear = cal.get(Calendar.YEAR);
	}
	
	switch (actMonth){
		case 0: prevMonth = 11;	nextMonth = 1; prevYear = actYear-1; nextYear = actYear; break;
		case 11: prevMonth = 10; nextMonth = 0; prevYear = actYear; nextYear = actYear + 1; break;
		default: prevMonth = actMonth-1; nextMonth = actMonth + 1; prevYear = actYear; nextYear = actYear; break;
	}
	%>
	<div class="content">
		<div class="monthButtonContainer">
			<%
			for(int i=1; i < 8; i++){
				int month=actMonth+i;
				int year=actYear;
				if(month > 11){
					month = month-12;
					year = year + 1;
				}
				%>
				<a class="button" href="/controller?page=calendar&month=<%= month %>&year=<%= year %>"><%= cu.getMonthForInt(month) + " " + year %></a>
				<%
			}
			%>
		</div>
		
		<div class="pager">
			<span class="marked">
				<a class="forwardBack" href="/controller?page=calendar&month=<%= prevMonth %>&year=<%= prevYear %>">&lt;</a>
			</span>
			<div class="displayMonth">
				<span class="marked"><%= sdf.format(now) %></span>
			</div>
			<span class="marked">
				<a class="forwardBack" href="/controller?page=calendar&month=<%= nextMonth %>&year=<%= nextYear %>">&gt;</a>
			</span>
		</div>
		
		<table align="center">
			<thead>
				<tr>
					<td>&nbsp;Montag&nbsp;</td>
					<td>&nbsp;Dienstag&nbsp;</td>
					<td>&nbsp;Mittwoch&nbsp;</td>
					<td>&nbsp;Donnerstag&nbsp;</td>
					<td>&nbsp;Freitag&nbsp;</td>
					<td>&nbsp;Samstag&nbsp;</td>
					<td>&nbsp;Sonntag&nbsp;</td>
				</tr>
			</thead>
			<tr class="week">
		
			<%
			// Add 1 hour to UCT
			now = cu.addHours(now, 1);
			GregorianCalendar startDay = new GregorianCalendar(Locale.GERMANY);
			startDay.setTime(cu.getStartDay(now));
			GregorianCalendar endDay = new GregorianCalendar(Locale.GERMANY);
			endDay.setTime(cu.getEndDay(now));
			// 1 Stunde hinzufügen, damit das Endedatum noch im Kalender aufgeführt wird
			endDay.add(Calendar.HOUR, 1);
			for(; startDay.getTime().before(endDay.getTime()); startDay.add(Calendar.HOUR, 24)){
			 	int day = startDay.get(Calendar.DAY_OF_MONTH);
			 	int month = startDay.get(Calendar.MONTH);
			 	int year = startDay.get(Calendar.YEAR);
				if(startDay.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
					%>
			</tr>
			<tr class="week">
					<%
				}%>
				<td class="day" onclick="location.href='/controller?page=detail&year=<%=year %>&month=<%=month %>&day=<%= day %>'">
					<% 
					if(month != actMonth){
						out.print("<div class=\"otherMonth\">");	
					}
					%>
						<a class="day" href="/controller?page=detail&year=<%=year %>&month=<%=month %>&day=<%= day %>">
							<%= day %>
						</a>
						<%
						List<Event> events = EventService.getEvents(day, month, year);
						for(Event e: events){
							%>
							<div style="border-top: 1px solid;">
								<%= e.getEventName() %>
							</div>
							<%
						}
						%>
					<% 
					if(month != actMonth){
						out.print("</div>");	
					} 
					%>
				</td>		
				<% 
			}
			%>
			</tr>
		</table>
	
		<div class="buttonContainer">
			<a class="button" href="/calendar.jsp">Zum Heute</a>
			<a class="button" href="/eventJSP/createEvent.jsp?year=<%= actYear %>&month=<%= actMonth %>&fromPage=calendar">
				Neuen Eintrag
			</a>
			<a class="button" href="/export.ics">ICS Export</a>
			<%
			if(false){
			%>
				<a class="button" href="/eventJSP/allEvents.jsp">Alle Eintr&auml;ge anzeigen</a>
				<a class="button" href="/deleteEvent?key=all">Alle Eintr&auml;ge l&ouml;schen</a>
				<a class="button" href="/importCSV.jsp">Eintr&auml;ge importieren</a>
			<%	
			}
			%>
		</div>
	</div>
</body>
</html>